package org.niux.AutoLoginQQ.ui;

import java.io.IOException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;

public class SuggestDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private String newVersion;
	private String downloadPath;
	private String status;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public SuggestDialog(Shell parent, int style, String status,
			String newVersion, String downloadPath) {
		super(parent, style);
		setText("SWT Dialog");
		this.newVersion = newVersion;
		this.downloadPath = downloadPath;
		this.status = status;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();

		// 窗口居中
		int width = shell.getMonitor().getClientArea().width;
		int height = shell.getMonitor().getClientArea().height;

		int x = shell.getSize().x;
		int y = shell.getSize().y;

		if (x > width) {
			shell.getSize().x = width;
		}

		if (y > height) {
			shell.getSize().y = height;
		}

		shell.setLocation((width - x) / 2, (height - y) / 2);

		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.SYSTEM_MODAL);
		shell.setSize(518, 196);
		shell.setText("建议");
		shell.setBackground(SWTResourceManager.getColor(173, 216, 230));
		
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite.setBounds(0, 0, 512, 123);

		Label label = new Label(composite, SWT.NONE);
		label.setBounds(10, 28, 492, 18);
		
		if (status.equals("false")) {
			label.setText("当前版本已过期，最新版本是：" + newVersion);
		} else {
			label.setText("当前最新版本是：" + newVersion);
		}

		label.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));

		Label lblNewLabel_1 = new Label(composite, SWT.NONE);
		lblNewLabel_1.setBounds(10, 52, 49, 18);

		if (status.equals("false")) {
			lblNewLabel_1.setText("请到");
		} else {
			lblNewLabel_1.setText("建议到");
		}
		
		lblNewLabel_1.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_WHITE));

		Link link = new Link(composite, SWT.NONE);
		link.setBounds(65, 52, 437, 18);
		link.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					Runtime.getRuntime().exec(
							"rundll32 url.dll,FileProtocolHandler "
									+ downloadPath);
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		link.setText("<a>" + downloadPath + "</a>");
		link.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));

		Label lblNewLabel_2 = new Label(composite, SWT.NONE);
		lblNewLabel_2.setBounds(10, 76, 96, 18);
		lblNewLabel_2.setText("下载最新版本使用");
		lblNewLabel_2.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_WHITE));

		Composite composite_1 = new Composite(shell, SWT.NONE);
		composite_1.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		composite_1.setBounds(0, 122, 512, 49);

		Button button = new Button(composite_1, SWT.NONE);
		button.setBounds(422, 10, 80, 27);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (status.equals("false")) {
					System.exit(0);
				} else {
					shell.close();
				}
			}
		});
		button.setText("确认");
		
	}
}
